/*
 *    Copyright 2010-2013 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package org.mybatis.jpetstore.web.actions;

import org.mybatis.jpetstore.domain.Order;
import org.mybatis.jpetstore.service.OrderService;
import org.mybatis.jpetstore.web.bean.AccountBean;
import org.mybatis.jpetstore.web.bean.CartBean;
import org.mybatis.jpetstore.web.bean.OrderBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author Eduardo Macarron
 */
@Controller
@RequestMapping("order")
@SessionAttributes("orderBean")
public class OrderController extends AbstractController {

	private static final long serialVersionUID = -6171288227470176272L;

	private static final String CONFIRM_ORDER = "/order/ConfirmOrder";
	private static final String LIST_ORDERS = "/order/ListOrders";
	private static final String NEW_ORDER = "/order/NewOrderForm";
	private static final String SHIPPING = "/order/ShippingForm";
	private static final String VIEW_ORDER = "/order/ViewOrder";

	private static final List<String> CARD_TYPE_LIST;

	@Autowired
	private OrderService orderService;

	static {
		List<String> cardList = new ArrayList<String>();
		cardList.add("Visa");
		cardList.add("MasterCard");
		cardList.add("American Express");
		CARD_TYPE_LIST = Collections.unmodifiableList(cardList);
	}


	@ModelAttribute("creditCardTypes")
	public List<String> getCreditCardTypes() {
		return CARD_TYPE_LIST;
	}

	@RequestMapping("listOrders")
	public String listOrders(HttpSession session) {
		AccountBean accountBean = (AccountBean) session.getAttribute("accountBean");
		List<Order> orderList = orderService.getOrdersByUsername(accountBean.getAccount().getUsername());
		return (LIST_ORDERS);
	}

	@RequestMapping("newOrderForm")
	public String newOrderForm(HttpSession session, ModelMap model) {
		AccountBean accountBean = (AccountBean) session.getAttribute("accountBean");
		CartBean cartBean = (CartBean) session.getAttribute("cartBean");

		if (accountBean == null || !accountBean.isAuthenticated()) {
			model.addAttribute("message", "You must sign on before attempting to check out.  Please sign on and try checking out again.");
			return ("redirect:/account/signon");
		} else if (cartBean != null) {
			Order order = new Order();
			order.initOrder(accountBean.getAccount(), cartBean.getCart());
			OrderBean orderBean = new OrderBean();
			orderBean.setOrder(order);
			model.addAttribute("orderBean", orderBean);
			return (NEW_ORDER);
		} else {
			model.addAttribute("message", "An order could not be created because a cart could not be found.");
			return (ERROR);
		}
	}

	@RequestMapping("newOrder")
	public String newOrder(HttpSession session, ModelMap model, @ModelAttribute("orderBean") OrderBean orderBean) {
		boolean shippingAddressRequired = orderBean.isShippingAddressRequired();
		if (shippingAddressRequired) {
			shippingAddressRequired = false;
			return (SHIPPING);
		} else if (!orderBean.isConfirmed()) {
			return (CONFIRM_ORDER);
		} else if (orderBean.getOrder() != null) {

			orderService.insertOrder(orderBean.getOrder());

			CartBean cartBean = (CartBean) session.getAttribute("cartBean");
			cartBean.clear();

			model.addAttribute("message", "Thank you, your order has been submitted.");

			return (VIEW_ORDER);
		} else {
			model.addAttribute("message", "An error occurred processing your order (order was null).");
			return (ERROR);
		}
	}

	@RequestMapping("viewOrder")
	public String viewOrder(HttpSession session, ModelMap model, @ModelAttribute OrderBean orderBean) {
		Order order = orderBean.getOrder();
		AccountBean accountBean = (AccountBean) session.getAttribute("accountBean");

		order = orderService.getOrder(order.getOrderId());
		if (accountBean.getAccount().getUsername().equals(order.getUsername())) {
			orderBean.setOrder(order);
			return (VIEW_ORDER);
		} else {
			orderBean.setOrder(null);
			model.addAttribute("message", "You may only view your own orders.");
			return (ERROR);
		}
	}

}
